package com.baichuan.util;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;

/**
 * @Auther: leo
 * @Date: 2020/4/6 15:32
 * @Description:
 */
public class DateUtil {

    public static final String FORMAT1 = "yyyy-MM-dd HH:mm:ss";

    public static final String FORMAT2 = "yyMMddHHmmss";

    public static final String FORMAT3 = "yyyy年MM月dd日";

    public static final String FORMAT4 = "yyyy-MM-dd";

    public static final String FORMAT5 = "yyMMdd";

    public static final String FORMAT_YYYY = "yyyy";

    public static final String FORMAT_MM = "MM";

    public static final String FORMAT_DD = "dd";

    public static final String FORMAT_MM_DD = "MM月dd日";

    public static final String FORMAT_yyMMDddHmm = "yyMMddHHmm";

    public static final String FORMAT_yyyy_mm = "yyyy-MM";

    /**
     * 根据格式获取日期字符串
     *
     * @param date
     * @param dateFormat
     * @return
     */
    public static String toStr(Date date, String dateFormat) {
        return new SimpleDateFormat(dateFormat).format(date);
    }

    /**
     * 默认 yyyy-MM-dd HH:mm:ss格式时间字符串
     *
     * @param date
     * @return
     */
    public static String toStr(Date date) {
        if (date == null)
            return null;
        return toStr(date, FORMAT1);
    }

    public static Date toDate(String date) {
        try {
            if (date == null || "".equals(date))
                return null;
            return toDate(date, FORMAT1);
        } catch (Exception e) {
            return null;
        }
    }

    public static Date toDate(String date, String dateFormat) throws ParseException {
        return new SimpleDateFormat(dateFormat).parse(date);
    }

    public static Date addDate(Date date, Integer field, Integer amount) {
        Calendar calendar = new GregorianCalendar();
        calendar.setTime(date);
        calendar.add(field, amount);
        return calendar.getTime();
    }

    /**
     * 计算两个日期相差天数
     *
     * @param begDate 开始时间
     * @param endDate 结束时间
     * @return
     * @throws ParseException
     */
    public static int diffDays(Date begDate, Date endDate) throws ParseException {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        String s1 = sdf.format(begDate);
        Date d1 = sdf.parse(s1);
        String s2 = sdf.format(endDate);
        Date d2 = sdf.parse(s2);
        return (int) ((d2.getTime() - d1.getTime()) / 1000 / 60 / 60 / 24);
    }

    /**
     * 计算两个日期相差小时数
     * @param begDate
     * @param endDate
     * @return
     * @throws Exception
     */
    public static long diffHour(Date begDate, Date endDate) {
        //按照传入的格式生成一个simpledateformate对象
        long nh = 1000 * 60 * 60;//一小时的毫秒数
        //获得两个时间的毫秒时间差异
        long diff = endDate.getTime() - begDate.getTime();
        return diff / nh;
    }

    /**
     * 获取前月的第一天
     *
     * @param date
     * @return
     */
    public static Date getMonthFirstDay(Date date) {
        Calendar cale = Calendar.getInstance();
        cale.setTime(date);
        cale.add(Calendar.MONTH, 0);
        cale.set(Calendar.DAY_OF_MONTH, 1);
        return cale.getTime();
    }


    /**
     * 获取前月的最后一天
     *
     * @param date
     * @return
     */
    public static Date getMonthLastDay(Date date) {
        Calendar cale = Calendar.getInstance();
        cale.setTime(date);
        cale.add(Calendar.MONTH, 1);
        cale.set(Calendar.DAY_OF_MONTH, 0);
        return cale.getTime();
    }

    /**
     * 判断两个日期是否同一个月
     *
     * @param date1
     * @param date2
     * @return
     */
    public static boolean equalsMonth(Date date1, Date date2) {
        Calendar calendar1 = Calendar.getInstance();
        calendar1.setTime(date1);
        Calendar calendar2 = Calendar.getInstance();
        calendar2.setTime(date2);
        return calendar1.get(Calendar.YEAR) == calendar2.get(Calendar.YEAR) && calendar1.get(Calendar.MONTH) == calendar2.get(Calendar.MONTH);
    }

    /**
     * 获取一天时间的0时或23时
     *
     * @param date
     * @param isBegin
     * @return
     */
    public static Date DateToStringBeginOrEnd(Date date, boolean isBegin) {
        Calendar calendar1 = Calendar.getInstance();
        calendar1.setTime(date);
        //获取某一天的0点0分0秒 或者 23点59分59秒
        if (isBegin) {
            calendar1.set(calendar1.get(Calendar.YEAR), calendar1.get(Calendar.MONTH), calendar1.get(Calendar.DAY_OF_MONTH),
                    0, 0, 0);
        } else {
            calendar1.set(calendar1.get(Calendar.YEAR), calendar1.get(Calendar.MONTH), calendar1.get(Calendar.DAY_OF_MONTH),
                    23, 59, 59);
        }
        return calendar1.getTime();
    }


    public static String getDatePoor(Date endDate, Date nowDate) throws ParseException {
        long nd = 1000l * 24 * 60 * 60;
        long nh = 1000l * 60 * 60;
        long nm = 1000l * 60;
        long ns = 1000l;
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        String s1 = sdf.format(endDate);
        Date d1 = sdf.parse(s1);
        String s2 = sdf.format(nowDate);
        Date d2 = sdf.parse(s2);
        // long ns = 1000;
        // 获得两个时间的毫秒时间差异
        long diff = d1.getTime() - d2.getTime();
        // 计算差多少天
        long day = diff / nd;
        // 计算差多少小时
        long hour = diff % nd / nh;
        // 计算差多少分钟
        long min = diff % nd % nh / nm;
        // 计算差多少秒//输出结果
        long sec = diff % nd % nh % nm / ns;
        String result = "";
        if (day > 0)
            result += day + "天";
        result += hour + ":" + min + ":" + sec;
        return result;
    }

    /**
     * 时间间隔
     *
     * @param startDate 开始时间
     * @param endDate   结束时间
     * @return
     */
    public static String getDistanceTimes(Date startDate, Date endDate) {
        long time1 = startDate.getTime();
        long time2 = endDate.getTime();
        long diff;
        String result = "";
        diff = time2 - time1;
        long day = diff / (24 * 60 * 60 * 1000);
        long hour = (diff / (60 * 60 * 1000) - day * 24);
        long min = ((diff / (60 * 1000)) - day * 24 * 60 - hour * 60);
        long sec = (diff / 1000 - day * 24 * 60 * 60 - hour * 60 * 60 - min * 60);
        if (day > 0) {
            result += day + "天";
            result += hour + ":" + min + ":" + sec;
        }
        return result;
    }

    /**
     * 计算两个日期相差年月
     *
     * @param date1
     * @param date2
     * @return
     */
    public static String getYearAndMonthNum(Date date1, Date date2) {
        Calendar c1 = Calendar.getInstance();
        Calendar c2 = Calendar.getInstance();

        c1.setTime(date1);
        c2.setTime(date2);

        int differenceMonth = c2.get(Calendar.MONTH) - c1.get(Calendar.MONTH);
        int yearMonth = (c2.get(Calendar.YEAR) - c1.get(Calendar.YEAR)) * 12;
        int months = Math.abs(differenceMonth + yearMonth);
        if (months == 0)
            months = 1;
        int year = months / 12;
        int month = months % 12;
        String result = "";
        if (year > 0)
            result += year + "年";
        if (month > 0)
            result += month + "个月";
        else
            result += "整";
        return result;
    }

    /**
     * 格式化时间发布时间
     *
     * @param date
     * @return
     */
    public static String formatPublishTime(Date date) throws ParseException {
        long value = Math.abs((new Date().getTime() - date.getTime()) / 1000);
        int differentDays = Math.abs(diffDays(date, new Date()));
        if (value < 60 * 60 * 1)
            return "刚刚";
        else if (differentDays == 0)
            return "今天";
        else if (differentDays == 1)
            return "昨天";
        else if (differentDays < 10)
            return differentDays + "天前";
        else
            return toStr(date, FORMAT_MM_DD) + "";
    }

    public static void main(String[] args) throws ParseException {
        System.out.println(formatPublishTime(new Date()));
    }

    public static int getDay(Date now) {
        Calendar c1 = Calendar.getInstance();
        c1.setTime(now);
        return c1.get(Calendar.DAY_OF_MONTH);
    }
}
